package com.taoban.shopdata.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taoban.shopdata.thread.AmazonGoodsCollectThread;
import com.yufei.entity.goods.Category;
import com.yufei.entity.goods.Goods;
import com.yufei.entity.goods.GoodsCategory;
import com.yufei.pfw.service.PfwService;
import com.yufei.pfw.service.QueryPaire;
import com.yufei.pfw.service.QueryServiceExcetion;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.ExceptionUtil;
import com.yufei.utils.SpringUtils;

public class MyOwnSaveGoodsImpl implements SaveGoods {
	PfwService pfwService=(PfwService) SpringUtils.getBeanFromBeanContainer(PfwService.class);
	private static final Log mLog = LogFactory.getLog(MyOwnSaveGoodsImpl.class);

	@Override
	public void saveGoods(Map goodsProperties) {
		// TODO Auto-generated method stub
		Boolean isUpdate=(boolean) goodsProperties.get("isUpdate");
		String offerListingId=(String) goodsProperties.get("offerListingId");
	
			if(isUpdate){
				List<QueryPaire> queryPaires=new ArrayList<>();
				String mallItemId = (String) goodsProperties.get("mallItemId");
				queryPaires.add(new QueryPaire("mallItemId", "is:"+mallItemId+""));
				try {
					Goods goods=(Goods) pfwService.query(queryPaires, Goods.class).get(0);
				       goods.setPrice(Double.parseDouble((String) goodsProperties.get("goodPrice")));
				       if(CommonUtil.isEmptyOrNull(offerListingId)){
					       goods.setStock(Goods.stock_noHave);

				       }
				       else{
					       goods.setStock(Goods.stock_have);

				       }
				       goods.setFindTime(System.currentTimeMillis());     
				       goods.setUpdatedTime(System.currentTimeMillis());

				       
				       pfwService.update(goods);
				       return;
				} catch (QueryServiceExcetion e) {
					// TODO Auto-generated catch block
					mLog.info("在对商品:"+mallItemId+"进行查询更行是出错！错误信息是:"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
				   return;
				}
				}	
		
	
	
       Goods goods=new Goods();
       goods.setCategoryId((String) goodsProperties.get("categoryId"));
       //			String goodDetailDes=null,goodTitle=null,goodPrice=null,goodUrl=null,asin=null;

       goods.setDesc((String) goodsProperties.get("goodDetailDes"));
       goods.setTitle((String) goodsProperties.get("goodTitle"));

       goods.setPrice(Double.parseDouble((String) goodsProperties.get("goodPrice")));

       goods.setUrl((String) goodsProperties.get("goodUrl"));

       goods.setMallItemId((String) goodsProperties.get("mallItemId"));
       goods.setMallId((String) goodsProperties.get("mallId"));
       if(CommonUtil.isEmptyOrNull(offerListingId)){
	       goods.setStock(Goods.stock_noHave);

       }
       else{
	       goods.setStock(Goods.stock_have);

       }
       goods.setFindTime(System.currentTimeMillis());     

       pfwService.save(goods);


       
	}
	public  void saveCatagory(GoodsCategory goodsCategory){
		pfwService.save(goodsCategory);
		if(CommonUtil.isEmptyOrNull(goodsCategory.getSubCategories())){
			return;
		}
		for(Category goodsCategory2:goodsCategory.getSubCategories()){
			saveCatagory((GoodsCategory) goodsCategory2);
		}
	}
public static void main(String[] args){
		GoodsCategory goodsCategory=(GoodsCategory) CommonUtil.getObjectFromXml(Thread.currentThread().getContextClassLoader().getResourceAsStream("categories.xml"), GoodsCategory.class);
		new MyOwnSaveGoodsImpl().saveCatagory(goodsCategory)
		;
		
		
	
}
}
